package com.lining.orangeservice.config;

import com.lining.orangeservice.filter.JwtAuthenticationFilter;
import com.lining.orangeservice.filter.PermissionAuthorizationFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

//@EnableWebSecurity,开启Spring Security
@Configuration
@EnableWebSecurity
public class SecurityConfig {

    @Autowired
    private  JwtAuthenticationFilter jwtAuthenticationFilter;
    
    @Autowired
    private PermissionAuthorizationFilter permissionAuthorizationFilter;

    // 白名单配置
    private static final String[] WHITE_LIST = {
            "/auth/login",
            "/login",
            "/auth/register",
            "/doc.html",// swagger-ui的入口页面
            "/webjars/**",// swagger-ui的静态资源
            "/v3/api-docs/**",// swagger-ui的接口文档
            "/swagger-resources/**",// swagger-ui的资源配置
            "/swagger-ui/**",        // 添加此行
            "/swagger-ui.html",      // 确保此项存在
            "/captcha/get" ,          // 添加验证码接口到白名单
            "/static/**"// 静态资源
    };

// 作用：配置Spring Security的过滤链，设置认证方式、授权方式、白名单等
    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http.csrf(csrf -> csrf.disable())// 关闭csrf
                .sessionManagement(session ->session.sessionCreationPolicy(SessionCreationPolicy.STATELESS)) // 禁止创建Session，使用无状态模式
                .authorizeHttpRequests(auth -> auth
                        .requestMatchers(WHITE_LIST).permitAll() // 白名单放行
                        .anyRequest().authenticated()) // 其他请求都需要认证
                //添加JWT认证过滤器，放在UsernamePasswordAuthenticationFilter之前,确保每个请求只被处理一次,避免重复认证
                //UsernamePasswordAuthenticationFilter是Spring Security的认证过滤器，用于处理用户名密码认证
                .addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class)
                // 添加权限授权过滤器，在JWT认证过滤器之后
                .addFilterAfter(permissionAuthorizationFilter, JwtAuthenticationFilter.class);
        return http.build();
    }

    // 创建AuthenticationManager，用于处理用户认证,AuthenticationManager是Spring Security的核心组件，用于处理用户认证
    //流程：1. 创建AuthenticationManager实例，可自定义创建方式
    @Bean
    public AuthenticationManager authenticationManager(
            org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration authenticationConfiguration) throws Exception {
        return authenticationConfiguration.getAuthenticationManager();
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

}
